<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class Wechatoffiaccounts extends Model
{
    protected $table = 'wechat_offiaccounts';
    protected $primaryKey = 'id';
    
    public static function getWechatOffiaccountsLists($name,$authority,$users_id)
    {
        $query = self::query();
        if (!empty($name)) $query->where("name","like","%".$name."%");
        if ($authority != 3) $query->where("users_id",$users_id);
        return $query->where("disable",1)->orderBy("id","DESC")->paginate(25);
    }

    public static function getWechatOffiaccountsByOriginalid($original_id,$authority,$users_id)
    {
        $query = self::where("original_id",$original_id)->where("disable",1);
        if ($authority != 3) $query->where("users_id",$users_id);
        return $query->orderBy("id","DESC")->first();
    }

    public static function usersWechatOffiaccounts($appid,$users_id)
    {
        return self::where("appid",$appid)->where("users_id",$users_id)->orderBy("id","DESC")->first();
    }

    public static function wechatOffiaccountsByAppid($appid)
    {
        return self::where("appid",$appid)->orderBy("id","DESC")->first();
    }

    public static function getUsersWechatOffiaccounts($users_id)
    {
        return self::select("appid","name")->where("users_id",$users_id)->orderBy("id","DESC")->get();
    }

    public static function wechatOffiaccountsByOriginalId($original_id)
    {
        return self::select("appid","secret","original_id")->where("original_id",$original_id)->orderBy("id","DESC")->first();
    }

    public static function wechatOffiaccountsInsert($users_id,$name,$appid,$secret,$original_id)
    {
        return self::insert(["users_id"=>$users_id,"name"=>$name,"appid"=>$appid,"secret"=>$secret,"original_id"=>$original_id,"disable"=>1,"created_at"=>time()]);
    }

    public static function wechatOffiaccountsAuthorizerInsert($users_id,$name,$appid,$original_id,$principal_name,$signature,$qrcode_url,$alias,$service_type_info,$verify_type_info,$avatar)
    {
        return self::insert(["users_id"=>$users_id,"name"=>$name,"appid"=>$appid,"original_id"=>$original_id,"principal_name"=>$principal_name,"signature"=>$signature,"qrcode_url"=>$qrcode_url,"alias"=>$alias,"service_type_info"=>$service_type_info,"verify_type_info"=>$verify_type_info,"avatar"=>$avatar,"disable"=>1,"created_at"=>time()]);
    }

    public static function wechatOffiaccountsAuthorizerUpdate($id,$name,$appid,$original_id,$principal_name,$signature,$qrcode_url,$alias,$service_type_info,$verify_type_info,$avatar)
    {
        return self::where("id",$id)->update(["name"=>$name,"appid"=>$appid,"original_id"=>$original_id,"principal_name"=>$principal_name,"signature"=>$signature,"qrcode_url"=>$qrcode_url,"alias"=>$alias,"service_type_info"=>$service_type_info,"verify_type_info"=>$verify_type_info,"avatar"=>$avatar]);
    }

    public static function wechatOffiacDisable($id,$disable)
    {
        return self::where("id",$id)->update(["disable"=>$disable]);
    }
}